package com.card.game;

import java.util.List;
import java.util.Set;
import java.util.Vector;

import com.card.common.CardContants;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;

/**   
 * @Description: TODO(用一句话描述该文件做什么) 
 * @author siyunlong  
 * @date 2016年10月10日 下午6:24:49 
 * @version V1.0   
 */
public class KdUtils {
	public static List<Card> getOutCardForTing(Vector<Card> cards) {
		Set<Card> setCard = Sets.newHashSet();
		List<Card> outs = Lists.newArrayList();
		for (int i = 0; i < cards.size(); i++) {
			Vector<Card> clone = (Vector<Card>)cards.clone();
			Card c = clone.remove(i);
			if(setCard.contains(c))
			 continue;
			if(isCanTingForCard(clone)) {
				outs.add(c);
			}
			setCard.add(c);
		}
		setCard.clear();
		return outs;
	}
	
	public static boolean isCanTing(List<Card> tings,List<Card> oldTings) {
		if(oldTings == null || tings == null) {
			return false;
		}
		if(oldTings.containsAll(tings)) {
			for (Card card : tings) {
				if(card.getIndex() >= CardContants.Min_Index || card.getType() >= CardType.DONG) {
					return true;
				}
			}
		}
		return false;
	}
	
	public static boolean isCanTingForCard(Vector<Card> cards) {
		for (int i = CardType.WAN; i <= CardType.BING; i++) {
			for (int j = CardContants.Min_Index; j < 10; j++) {
				Card c = new Card(i, j);
				if(isHu(cards, c)) {
					return true;
				}
			}
		}
		for (int i = CardType.DONG; i <= CardType.BAI; i++) {
			Card c = new Card(i, 0);
			if(isHu(cards, c)) {
				return true;
			}
		}
		for (int j = 1; j < 4; j++) {
			Card c = new Card(CardType.Jian, j);
			if(isHu(cards, c)) {
				return true;
			}
		}
		return false;
	}
	public static List<Card> getOutCardForTingFengHu(Vector<Card> cards) {
		List<Card> tingCards = getOutCardForTing(transCards(cards));
		return fanTransCards(tingCards);
	}
	public static Vector<Card> transCards(Vector<Card> cards) {
		Vector<Card> clones = (Vector<Card>)cards.clone();
		for (int i = clones.size()-1; i >= 0; i--) {
			Card card = clones.get(i);
			if(card.isJianCard()) {
				clones.remove(i);
				Card temp = null;
                if(card.getIndex() == 1)
				 temp = new Card(CardType.ZHONG, 0);
	              if(card.getIndex() == 2)
			     temp = new Card(CardType.FA, 0);
                 if(card.getIndex() == 3) 
				temp = new Card(CardType.BAI, 0);
				clones.add(i,temp);
			}
		}
		return clones;
	}
	
	public static List<Card> fanTransCards(List<Card> clones) {
		for (int i = clones.size()-1; i >= 0; i--) {
			Card card = clones.get(i);
			int type = card.getType();
			if(type == CardType.ZHONG || type == CardType.FA || type == CardType.BAI) {
				clones.remove(i);
				Card temp = null;
							
							
				if(type == CardType.ZHONG) 
				temp = new Card(CardType.Jian, 1);
				if(type == CardType.FA) 
				temp = new Card(CardType.Jian, 2);
				if(type == CardType.BAI)
				 temp = new Card(CardType.Jian, 3);

				clones.add(i,temp);
			}
		}
		return clones;
	}

	public static List<Card> getTingCards(Vector<Card> cards, Card hunzi) {
		System.out.println("混子听牌:" + hunzi);
		List<Card> tings = Lists.newArrayList();
		if (hunzi.getType() > 0) {
			Vector<Card> temCards = new Vector<>();
			int hunCount = 0;
			for (int i = 0; i < temCards.size(); i++) {
				Card card = temCards.get(i);
				if (card.getType() == hunzi.getType() && card.getIndex() == hunzi.getIndex()) {
					hunCount++;
					continue;
				}
				temCards.add(card);
			}
			if (hunCount == 1) {
				for (int i = CardType.WAN; i <= CardType.BING; i++) {
					for (int j = 3; j < 10; j++) {
						Vector<Card> clone = (Vector<Card>) temCards.clone();
						clone.add(new Card(i, j));
						List<Card> tingCards = getTingCards(clone);
						for (Card card : tingCards) {
							if (!tings.contains(card)) {
								tings.add(card);
							}
						}
					}
				}
				for (int i = CardType.DONG; i <= CardType.BAI; i++) {
					Vector<Card> clone = (Vector<Card>) temCards.clone();
					clone.add(new Card(i, 0));
					List<Card> tingCards = getTingCards(clone);
					for (Card card : tingCards) {
						if (!tings.contains(card)) {
							tings.add(card);
						}
					}
				}
			}

		} else {
			tings = getTingCards(cards);
		}
		return tings;
	}

	// gp获取ting牌的值s
	public static List<Card> getTingCards(Vector<Card> cards) {
		List<Card> tings = Lists.newArrayList();
		for (int i = CardType.WAN; i <= CardType.BING; i++) {
			for (int j = 3; j < 10; j++) {
				Card c = new Card(i, j);
				if(isHu(cards, c)) {
					tings.add(c);
				}
			}
		}
		for (int i = CardType.DONG; i <= CardType.BAI; i++) {
			Card c = new Card(i, 0);
			if(isHu(cards, c)) {
				tings.add(c);
			}
		}
		return tings;
	}
	public static List<Card> getTingCardsForFeng2(Vector<Card> cards) {
		return fanTransCards(getTingCards(transCards(cards)));
	}
	public static List<Card> getTingCardsForFeng(Vector<Card> cards) {
		List<Card> tings = Lists.newArrayList();
		for (int i = CardType.WAN; i <= CardType.BING; i++) {
			for (int j = 3; j < 10; j++) {
				Card c = new Card(i, j);
				if(isHuFeng(cards, c)) {
					tings.add(c);
				}
			}
		}
		for (int i = CardType.DONG; i <= CardType.BEI; i++) {
			Card c = new Card(i, 0);
			if(isHuFeng(cards, c)) {
				tings.add(c);
			}
		}
		for (int j = 1; j < 4; j++) {
			Card c = new Card(CardType.Jian, j);
			if(isHuFeng(cards, c)) {
				tings.add(c);
			}
		}
		return tings;
	}
	private static boolean isHuFeng(Vector<Card> cards,Card temp) {
		Vector<Card> clone = (Vector<Card>)cards.clone();
		clone.add(temp);
		if(!clone.containsAll(YzUtils.JianCard)) {
			return false;
		}
		return HandUtils.isHuPai(clone);
	}
	public static boolean isHu(Vector<Card> cards,Card temp) {
		Vector<Card> clone = (Vector<Card>)cards.clone();
		clone.add(temp);
		return HandUtils.isHuPai(clone);
	}
	
	private static boolean isSameType(Vector<Card> sours,int type) {
		for (Card card : sours) {
			if(card.getType() != type) {
				return false;
			}
		}
		return true;
	}
	
	public static boolean isQingyise(Vector<Card> sours) {
		for (int i = CardType.WAN; i <= CardType.BING; i++) {
			if(isSameType(sours, i)) {
				return true;
			}
		}
		return false;
	}
	
	private static boolean isTiaoLong(Vector<Card> cards,int type) {
		Vector<Card> sours = (Vector<Card>)cards.clone();
		for (int j = 1; j < 10; j++) {
			if(!sours.remove(new Card(type, j))) {
				return false;
			}
		}
		return HandUtils.isHuPai(sours);
	}
	public static boolean isTiaoLong(Vector<Card> sours) {
		for (int i = CardType.WAN; i <= CardType.BING; i++) {
			if(isTiaoLong(sours, i)) {
				return true;
			}
		}
		return false;
	}
	
	public static boolean isDaDui(Vector<Card> sours) {
		if(sours.size() != 14 || !HandUtils.allIsDui(sours)) {
			return false;
		}
		Vector<Card> sour = (Vector<Card>)sours.clone();
		Vector<Hand> same4 = CardUtils.findSameCardBySize(sour, 4);
		return !same4.isEmpty();
	}
	
	private static final ImmutableList<Card> Yaos = ImmutableList.of(new Card(4,0),new Card(5,0),new Card(6,0),
	new Card(7,0),new Card(8,1),new Card(8,2),new Card(8,3),new Card(1,1),new Card(1,9),
			
		new Card(2,1),new Card(2,9),	new Card(3,1),new Card(3,9));
	
	public static boolean is13Yao(Vector<Card> sours) {
		Vector<Card> sour = (Vector<Card>)sours.clone();
		if(sour.containsAll(Yaos)) {
			for (Card card : Yaos) {
				sour.remove(card);
			}
			if(sour.size() != 1) {
				return false;
			}
			return Yaos.contains(sour.get(0));
		}
		return false;
	}
	
	public static void main(String[] args) {
//		Vector<Card> cards = new Vector<>();
//		cards.add(new Card(CardType.Jian, 1));
//		cards.add(new Card(CardType.BAI, 1));
//		cards.add(new Card(CardType.Jian, 2));
//		cards.add(new Card(CardType.BAI, 1));
//		cards.add(new Card(CardType.Jian, 3));
//		Vector<Card> clones = transCards(cards);
//		CardUtils.printCards(cards);
//		CardUtils.printCards(clones);
		String source = "1-1;1-2;1-3;1-4;1-5;1-6;1-7;1-8;1-9;1-9;1-9;";
		Vector<Card> cards = CardUtils.createCardsFromStr(source);
		System.err.println(isTiaoLong(cards));
//		System.err.println(isTiaoLong(cards));
//		System.err.println(isTiaoLong(cards));
//		Player player = new Player();
//		player.initCards(cards);
//		player.setLastInCard(new Card(1, 6));
//		player.ting = true;
//		player.getAnGang();
//		List<Card> cc = getTingCardsForFeng2(cards);
//		for (Card card : cc) {
//			System.err.println(card);
//		}
	}
}
